package cn.lili.modules.promotion.serviceimpl;

import cn.lili.common.security.AuthUser;
import cn.lili.common.utils.DateTimeConversionExample;
import cn.lili.common.utils.DtoToVoConverter;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.member.client.UserClient;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.promotion.entity.dos.BlindboxPrizePaper;
import cn.lili.modules.promotion.entity.dos.BlindboxPrizeRecord;
import cn.lili.modules.promotion.entity.dos.BlindboxRule;
import cn.lili.modules.promotion.entity.dto.CallbackBlindboxDTO;
import cn.lili.modules.promotion.entity.vos.BlindboxPrizeRecordVO;
import cn.lili.modules.promotion.mapper.BlindboxPrizePaperMapper;
import cn.lili.modules.promotion.mapper.BlindboxPrizeRecordMapper;
import cn.lili.modules.promotion.service.BlindboxPrizePaperService;
import cn.lili.modules.promotion.service.BlindboxPrizeRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 盲盒开奖记录 业务实现
 *
 * @author xiangqian
 */
@Service
@RequiredArgsConstructor
public class BlindboxPrizeRecordServiceImpl extends ServiceImpl<BlindboxPrizeRecordMapper, BlindboxPrizeRecord> implements BlindboxPrizeRecordService {

    @Resource
    private UserClient userClient;
    @Resource
    private BlindboxPrizePaperMapper blindboxPrizePaperMapper;

    /**
     * 查询最近一次公开的开奖记录
     */
    @Override
    public BlindboxPrizeRecord getBlindboxPrizeRecord() {
        //构造查询条件
        LambdaQueryWrapper<BlindboxPrizeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlindboxPrizeRecord::getType, true);
        queryWrapper.eq(BlindboxPrizeRecord::getDeleteFlag, false);
        queryWrapper.orderByDesc(BlindboxPrizeRecord::getCreateTime);
        queryWrapper.last("limit 1");

        //执行查询操作
        BlindboxPrizeRecord blindboxPrizeRecord = this.baseMapper.selectOne(queryWrapper);
        return blindboxPrizeRecord;
    }

    /**
     * 根据商品ID查询开奖记录
     *
     * @param goodsId 商品ID
     */
    @Override
    public BlindboxPrizeRecord getBlindboxPrizeRecord(String goodsId) {
        //构造查询条件
        LambdaQueryWrapper<BlindboxPrizeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlindboxPrizeRecord::getDeleteFlag, false);
        queryWrapper.eq(BlindboxPrizeRecord::getGoodsId, goodsId);
        queryWrapper.last("limit 1");

        //执行查询操作
        BlindboxPrizeRecord blindboxPrizeRecord = this.baseMapper.selectOne(queryWrapper);
        if (blindboxPrizeRecord != null) {
            return blindboxPrizeRecord;
        }
        return blindboxPrizeRecord;
    }

    /**
     * 查询我的中将记录
     *
     * @param currentUser 用户
     * @param goodsId     商品ID
     */
    @Override
    public BlindboxPrizeRecordVO getPrizerecordInfo(AuthUser currentUser, String goodsId) {
        //构造查询条件
        LambdaQueryWrapper<BlindboxPrizeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlindboxPrizeRecord::getDeleteFlag, false);
        queryWrapper.eq(BlindboxPrizeRecord::getGoodsId, goodsId);
        queryWrapper.eq(BlindboxPrizeRecord::getType, true);
        queryWrapper.eq(BlindboxPrizeRecord::getUserId,currentUser.getExtendId());
        queryWrapper.last("limit 1");
        BlindboxPrizeRecord blindboxPrizeRecord = this.baseMapper.selectOne(queryWrapper);
        if (blindboxPrizeRecord == null) {
            return null;
        }

        //执行查询操作
        BlindboxPrizeRecordVO vo = new BlindboxPrizeRecordVO().initBlindboxPrizeRecord(blindboxPrizeRecord);
        vo.setUserNick(currentUser.getNickName());
        vo.setFace(currentUser.getFace());
        return vo;
    }

    /**
     * 查询往期开奖记录
     */
    @Override
    public List<BlindboxPrizeRecordVO> getPrizerecordAll() {
        //构造查询条件
        LambdaQueryWrapper<BlindboxPrizeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlindboxPrizeRecord::getDeleteFlag, false);
        queryWrapper.eq(BlindboxPrizeRecord::getType, true);
        queryWrapper.orderByDesc(BlindboxPrizeRecord::getPrizeTime);

        //执行查询操作
        List<BlindboxPrizeRecord> list = this.baseMapper.selectList(queryWrapper);
        if (list == null || list.size() < 1) {
            return null;
        }
        List<BlindboxPrizeRecordVO> voList = DtoToVoConverter.convert(list, BlindboxPrizeRecordVO.class);
        return voList;
    }

    /**
     * 新增开奖记录
     *
     * @param callbackBlindboxDTO 订单信息
     * @param blindboxRule       盲盒规则
     * @param code                中奖Code
     */
    @Override
    public void insertBlindboxPrizeRecord(CallbackBlindboxDTO callbackBlindboxDTO, BlindboxRule blindboxRule, String code) {
        //查询开奖是否已存在
        LambdaQueryWrapper<BlindboxPrizePaper> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlindboxPrizePaper::getGoodsId,callbackBlindboxDTO.getGoodsId());
        List<BlindboxPrizePaper> blindboxPrizePapers = blindboxPrizePaperMapper.selectList(queryWrapper);
        if(blindboxPrizePapers!=null&&blindboxPrizePapers.size()>=1){
            return;
        }

        BlindboxPrizeRecord blindboxPrizeRecord = new BlindboxPrizeRecord();
        if(StringUtils.isNotEmpty(code)){
            //根据Code查询奖卷对应userId
            LambdaQueryWrapper<BlindboxPrizePaper> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(BlindboxPrizePaper::getCode, code);
            lambdaQueryWrapper.eq(BlindboxPrizePaper::getDeleteFlag, false);
            BlindboxPrizePaper blindboxPrizePaper = blindboxPrizePaperMapper.selectOne(lambdaQueryWrapper);
            //根据userId查询对应的用户信息
            User user = userClient.getById(blindboxPrizePaper.getUserId());
            blindboxPrizeRecord.setUserId(user.getId());
            blindboxPrizeRecord.setUserName(user.getUsername());
            blindboxPrizeRecord.setUserNick(user.getNickName());
        }
        blindboxPrizeRecord.setPrizeTime(DateTimeConversionExample.getTime(blindboxRule.getStartPrizeTime()));
        blindboxPrizeRecord.setType(false);
        blindboxPrizeRecord.setGoodsId(callbackBlindboxDTO.getGoodsId());
        blindboxPrizeRecord.setPaperCode(code);
        blindboxPrizeRecord.setPrizeName(blindboxRule.getPrizeName());
        blindboxPrizeRecord.setPrizeImage(blindboxRule.getPrizeImage());
        blindboxPrizeRecord.setGoodsName(callbackBlindboxDTO.getGoodsName());

        this.baseMapper.insert(blindboxPrizeRecord);
    }
}